var app = getApp();
Page({
  data: {
    userArray: [],
    defaultSize: 'default',
    disabled: false,
    loading: false,
    grzx: app.myUlife.ulifeUrl + 'wx/grzx.jpg',
    
    nickName:'未授权',
    avatarUrl: app.myUlife.ulifeUrl + 'wx/defaultimg.jpg',
    showsqModalStatus: false,//提示弹出
  },

  onLoad: function () {
    var that = this;
    var openid = wx.getStorageSync('openid');
    var pages = getCurrentPages();
    var prevPage = pages[pages.length -2];  //上一个页面
    var stus = prevPage.data.stus;
    that.setData({
      userArray: stus
    });
    if (openid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } 
    
  },



  buttona: function (e) {
    wx.removeStorageSync('name');
    //wx.removeStorageSync('identity');
    wx.removeStorageSync('nj');
    wx.removeStorageSync('bj');
    wx.removeStorageSync('usid');
    wx.removeStorageSync('dlzh');
    wx.removeStorageSync('sex');
    wx.removeStorageSync('user_type');
    wx.removeStorageSync('basic_grade_id');
    wx.removeStorageSync('basic_clbum_id');
    
    var index = e.target.dataset.index;
    var userArray = this.data.userArray;
    var dlzh = ''
    var dlma = ''
    for (var i = 0; i < userArray.length;i++){
      if (index==i){
        // 重新登录
        dlzh = userArray[i].dlzh
        dlma = userArray[i].dlma
      }
    }
    const that = this
    app.wxpost({
      url: 'wechart/selectUaccount.html',
      data: {
        dlzh: dlzh,
        dlma: dlma
      },
      success: function (res) {
        const data = res.data
        if (data.result) {
            wx.setStorageSync('name', data.name);
            //wx.setStorageSync('identity', data.zw);
            wx.setStorageSync('nj', data.nj);
            wx.setStorageSync('bj', data.bj);
            wx.setStorageSync('usid', data.usid);
            wx.setStorageSync('dlzh', data.dlzh);
            wx.setStorageSync('user_type',data.wstate);
            wx.setStorageSync('sex', data.sex);
            wx.setStorageSync('tokenJwc', data.token);
              // 教师需要知道年级id和班级id
              wx.setStorageSync('basic_grade_id', data.basic_grade_id);
              wx.setStorageSync('basic_clbum_id', data.basic_clbum_id);
            //相当于直接登录
            wx.switchTab({
              url: '../../pages/home/home'
            })
        }else{
          wx.redirectTo({
            url: '../../pages/login/login'
          })
        }
      }
    })
  },


  getUserInfo (e) {
    console.log(e)
    var that = this
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res) {
              console.log(res)
              wx.hideLoading(res.userInfo);
                 //加入缓存
              wx.setStorageSync('city', res.userInfo.city);
              wx.setStorageSync('province', res.userInfo.province);
              wx.setStorageSync('gender', res.userInfo.gender);
              wx.setStorageSync('nickName', res.userInfo.nickName);
              wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl);

              that.setData({
                gender: res.userInfo.gender,
                avatarUrl: res.userInfo.avatarUrl,
                nickName: res.userInfo.nickName
              });
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
            }
          })

        }
      }
    })
  }

})